/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef DESKTOPDATAPROVIDERPRIVATE_H
#define DESKTOPDATAPROVIDERPRIVATE_H

#include <QObject>
#include <QDir>
#include "desktopfilemanager.h"
#include "config.h"
#include "appcategorymanager.h"
#include "taskbarappmanager.h"
#include "uninstaller.h"
#include "types.h"

class BaseItem;
class DataBase;
class DesktopItemLoader;
namespace AppWidget {
class KAppWidgetManager;
}
namespace UKUI {
class IconTheme;
class DesktopDataProvider;

class DesktopDataProviderPrivate : public QObject
{
    Q_OBJECT
    Q_DECLARE_PUBLIC(DesktopDataProvider)

public:
    explicit DesktopDataProviderPrivate(DesktopDataProvider *parent = nullptr);

    struct DesktopDataHolder {
        QMap<UKUI::ItemId, BaseItem *> allItems;
        QMap<UKUI::ItemId, UKUI::ItemIdsPage> itemIdsInSet;
        QMap<int, UKUI::ItemIdInPageList> allItemIdsInHomeScreen;
        QList<UKUI::ItemId> appWidgetIds;
        QMap<int, UKUI::ItemIdsPage> itemIdsInPageGrid;
    };

    DesktopDataHolder dataHolder;
    DesktopFileManager m_desktopFileManager;
    AppWidget::KAppWidgetManager* m_appWidgetManager;
    DesktopItemLoader *m_desktopItemLoader;
    DataBase *database;
    IconTheme *icon_theme;
    int desktopRow = 0;
    int desktopColumn = 0;
    Config m_config;
    TaskbarAppManager m_taskbarAppManager;
    AppCategoryManager m_appCategoryManager;
    Uninstaller uninstaller;
    const int kTaskbarGroupId = 0;

    DesktopDataProvider *q_ptr;

private slots:
    void onDesktopFileAdded(const QString &desktopFile);
    void onDesktopFileDeteled(const QString &desktopFile);
    void onTaskbarAppChanged(const QStringList &taskbarDesktopName, TaskbarAppManager::TaskbarAction action);
    void onAppUninstalled(const QString &desktopFile);
    void onAppUninstallFailed(const QString &desktopFile);
    void onAppWidgetAdded(const QString &appWidgetName);
    void onAppWidgetDeleted(const QString &appWidgetName);

private:
    void removeWidgetByItemId(const int itemId);
    Widget* createWidgetItem(const QString &widgetName);
    void addWidgetItem(Widget* appWidget);
    void addWidgetItemToHomePage(const int itemId,
                                 const QList<int> &widgetPlaceHolder);

    void registerMetaType();
    void initConnections();
    void initAppwidgetConnections();
    void onIconThemeChanged();

    QList<ItemId> sortAppIdAccordingLetter(QList<UKUI::ItemId> &unorderedIdList);
    void updateAllAppCategory();

    void openDatabase();
    void loadLocalItems();
    int calculateIdForNewItem();
    void removeItem(int itemId);
    void removeItemByDesktopFile(const QString &desktopFile);
    void addItemByDesktopFile(const QString &desktopFile);
    void removeItemFromSet(int itemId, int setId, int pageIndex);
    void updateAfterRemoveFromHomeScreenSet(int setId, int pageIndex);
    void deleteEmptyPageFromSet(int emptyPageIndex, int setId);
    void removeItemFromHomeScreen(int itemId, int pageIndex);
    void resetItemIdInPageGrid(int itemId);
    void replaceIdInPageGrid(int pageIndex, int idBeforeChange, const int idAfterChange);
    void loadItemsFromDatabase();
    void loadNewInstalledApp();
    void checkItems();
    void updateTaskbarItems();
    void checkAppIconItem(int itemId);
    void checkGroupItem(int groupId);
    void placeItemsFromDatabase();
    void placeItem(int itemId, int page, int rowSpan, int columnSpan);
    void saveItemsToDatabase();
    void saveItemDetailsToDatabase(int id);
    void saveItemIdsInHomeScreenPageToDatabase(int page, const QList<int> &itemIds);
    void updateItemIdsInHomeScreenPageToDatabase(int page);
    QList<int> filterId(const QList<int> &ids);
    void updateItemPositionAfterCreateGroup(int underItemId, int aboveItemId, int groupId);
    void updateItemIdsInPageGridAfterCreateGroup(int underItemId, int aboveItemId, int groupId);
    void updateItemIdsInPageGridFromHomeScreenIds(int page);
    void adjustDesktopAppIdOrderAfterUpdateHomeScreenIds(int page);
    // exist, index, pageIndex
    std::tuple<bool, int, int> itemPositionInHomeScreen(int itemId) const;
    int itemPageIndex(int itemId);
    int appIdTaskbarChange(const QList<int> beforeChange,
                           const QList<int> afterChange,
                           TaskbarAppManager::TaskbarAction action);
    void moveAppIdFromSetToTaskbar(const int itemId, const QList<int> &taskbarAppIdsAfterChange);
    void moveAppIdFromHomeScreenToTaskbar(const int itemId, const QList<int> &taskbarAppIdsAfterChange);
    void moveAppIdFromTaskbarToHomeScreen(const int itemId);
    void removeAppIdFromSet(int itemId, int setId, int pageIndex);
    void removeAppIdFromHomeScreen(int itemId, int pageIndex);
    void deleteEmptyPageFromHomeScreen(int emptyPageIndex);
    void takeAppIdFromSet(int itemId);
    void takeAppIdFromHomeScreen(int itemId);
    void takeAppIdFromTaskbar(int itemId);
    void moveAppIdToHomeScreen(int itemId);
    void moveAppIdToTaskbar(const int itemId, const QList<int> &pageListAfterChange);

    void taskbarAppAdded(const QList<UKUI::ItemId> &newIds);
    void taskbarAppDeleted(const QList<UKUI::ItemId> &newIds);
    void taskbarAppMoved(const QList<UKUI::ItemId> &newIds);

    void saveNewGroupData(int groupId, const QString &groupName, const QList<int> &itemIds);
    // name2database,translatedName
    std::tuple<QString, QString> translateGroupName(const QString &groupName, int underItemId, int aboveItemId);
    void writeTaskbarDataToTaskbarPanelConf(const QList<int> &taskbarIds);
    QString getDatabasePath(const QString &datebaseDir);    
};

}

#endif // DESKTOPDATAPROVIDERPRIVATE_H
